package com.fulihui.information.biz.content.impl.handler;

import com.fulihui.information.biz.content.ObtainAppListPageHandler;
import com.fulihui.information.enums.EDataObtainType;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * APP 内容列表分页处理类注册，通过spring控制单例
 *
 * @author Willard.Hu on 2017/1/12 0012.
 */
@Component
public class AppListPageObtainHandlerRegister extends ApplicationObjectSupport {

    private Map<EDataObtainType, ObtainAppListPageHandler> handlerMap = new HashMap<>();

    @PostConstruct
    private void init() {
        Map<String, AbstractAppListPageObtainHandler> beans = getApplicationContext()
                .getBeansOfType(AbstractAppListPageObtainHandler.class);
        if (!isEmpty(beans)) {
            beans.forEach((k, v) -> handlerMap.put(v.obtainType(), v));
        }
    }

    public Optional<ObtainAppListPageHandler> get(EDataObtainType type) {
        ObtainAppListPageHandler handler = handlerMap.get(type);
        return Optional.ofNullable(handler);
    }
}
